//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using System.Text;

namespace CommunityServer.Controls 
{
	// *********************************************************************
	//  RatePost
	//
	/// <summary>
	/// </summary>
	/// 
	// ********************************************************************/
    public abstract class RatingControl : Control
    {
        CSContext csContext = CSContext.Current;

        #region Abstract Properties/Methods

		public abstract string ViewDetailsScript { get; set; }

		public abstract double CurrentRating { get; set; }

		public abstract int CurrentVotes { get; set; }

		public abstract bool UserCanRate { get; }

		public abstract void Rate(double value);

		#endregion

		#region Properties

		public virtual string CssClass
		{
			get
			{
				string state = (string) ViewState["CssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["CssClass"] = value;
			}
		}

		public virtual string ActiveCssClass
		{
			get
			{
				string state = (string) ViewState["ActiveCssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["ActiveCssClass"] = value;
			}
		}

		public virtual string ReadOnlyCssClass
		{
			get
			{
				string state = (string) ViewState["ReadOnlyCssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["ReadOnlyCssClass"] = value;
			}
		}

		public virtual string ImagesBaseUrl
		{
			get
			{
				string state = (string) ViewState["ImagesBaseUrl"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["ImagesBaseUrl"] = value;
			}
		}

		public virtual bool IsReadOnly
		{
			get
			{
				object state = ViewState["IsReadOnly"];
				if (state == null)
					return false;
				else
					return (bool) state;
			}
			set
			{
				ViewState["IsReadOnly"] = value;
			}
		}

		public virtual bool EnableDetails
		{
			get
			{
				object state = ViewState["EnableDetails"];
				if (state == null)
					return true;
				else
					return (bool) state;
			}
			set
			{
				ViewState["EnableDetails"] = value;
			}
		}

        #endregion

        #region Event Handlers

		protected virtual bool MakeInvisible()
		{
			return ((this.IsReadOnly || !this.UserCanRate) && this.CurrentVotes == 0);
		}

		protected override void OnInit(EventArgs e) 
		{
                if(!this.csContext.User.IsAnonymous)
			        AjaxManager.Register(this,"RatingControlAJAX");

			base.OnInit(e);
		}

		protected override void OnPreRender(EventArgs e)
		{
			if (MakeInvisible())
			{
				this.Visible = false;
				return;
			}

			if (!this.Page.IsClientScriptBlockRegistered(this.GetType().FullName))
				this.Page.RegisterClientScriptBlock(this.GetType().FullName, string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>",this.ResolveUrl("~/utility/ratingcontrol.js")));

			StringBuilder script = new StringBuilder();
			script.Append("<script language=\"javascript\">var ");
			script.Append(this.ClientID);
			script.Append("=new RateControl('");
			script.Append(this.ClientID);
			script.Append("','");
			script.Append(this.ClientID);
			script.Append("','");
			script.Append(this.ClientID);
			script.Append("_Value', 5, '");
			script.Append(this.Context.Response.ApplyAppPathModifier(this.ImagesBaseUrl));
			script.Append("', false, '");
			script.Append(this.CssClass);
			script.Append("','");
			script.Append(this.ActiveCssClass);
			script.Append("','");
			script.Append(this.ReadOnlyCssClass);
			script.Append("',['");
			script.Append(ResourceManager.GetString("PostRating_Zero").Replace("'", "\'"));
			script.Append("','");
			script.Append(ResourceManager.GetString("PostRating_One").Replace("'", "\'"));
			script.Append("','");
			script.Append(ResourceManager.GetString("PostRating_Two").Replace("'", "\'"));
			script.Append("','");
			script.Append(ResourceManager.GetString("PostRating_Three").Replace("'", "\'"));
			script.Append("','");
			script.Append(ResourceManager.GetString("PostRating_Four").Replace("'", "\'"));
			script.Append("','");
			script.Append(ResourceManager.GetString("PostRating_Five").Replace("'", "\'"));
			script.Append("'],");
			script.Append((!this.IsReadOnly && this.UserCanRate) ? "false" : "true");
			script.Append(",");
			script.Append(this.IsReadOnly ? "false" : "true");
			script.Append(");</script>");

            this.Page.RegisterStartupScript(this.ClientID, script.ToString());		

			base.OnPreRender(e);
		}

		protected override void Render(HtmlTextWriter writer)
		{
			writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
			writer.AddAttribute(HtmlTextWriterAttribute.Title, GetTitleText());
			if ((this.IsReadOnly || !this.UserCanRate) && this.EnableDetails && !Globals.IsNullorEmpty(this.ViewDetailsScript))
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Onclick, this.ViewDetailsScript);
				writer.AddAttribute(HtmlTextWriterAttribute.Style, "cursor: pointer;");
			}

			writer.RenderBeginTag(HtmlTextWriterTag.Span);
			writer.RenderEndTag();

			writer.AddAttribute(HtmlTextWriterAttribute.Type, "hidden");
			writer.AddAttribute(HtmlTextWriterAttribute.Value, this.CurrentRating.ToString());
			writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_Value");
			writer.RenderBeginTag(HtmlTextWriterTag.Input);
			writer.RenderEndTag();
		}

		protected virtual string GetTitleText()
		{
			string text = "?";

			switch ((int) System.Math.Round(this.CurrentRating))
			{
				case 0:
					text = ResourceManager.GetString("PostRating_Zero");
					break;

				case 1:
					text = ResourceManager.GetString("PostRating_One");
					break;

				case 2:
					text = ResourceManager.GetString("PostRating_Two");
					break;

				case 3:
					text = ResourceManager.GetString("PostRating_Three");
					break;

				case 4:
					text = ResourceManager.GetString("PostRating_Four");
					break;

				case 5:
					text = ResourceManager.GetString("PostRating_Five");
					break;
			}

			if (this.CurrentVotes > 0)
			{
				if (this.EnableDetails) 
				{
					if (CurrentRating == 0)
						return string.Format(ResourceManager.GetString("PostRating_AltText"), text, "0", CurrentVotes);
					else
						return string.Format(ResourceManager.GetString("PostRating_AltText"), text, this.CurrentRating.ToString("#.##"), this.CurrentVotes);
				} 
				else 
				{
					if (CurrentRating == 0)
						return string.Format(ResourceManager.GetString("Rating_AltText"), text, "0");
					else
						return string.Format(ResourceManager.GetString("Rating_AltText"), text, this.CurrentRating.ToString("#.##"));
				}
			}
			else
				return ResourceManager.GetString("Rating_NoRatings");
		}

		[AjaxMethod(IncludeControlValuesWithCallBack=true)]
		public virtual string[] SaveRating(double rating)
		{
			this.Rate(rating);
			return new String[] {this.CurrentRating.ToString(), this.GetTitleText(), this.EnableDetails ? this.ViewDetailsScript : "", this.UserCanRate ? "true" : "false" };
		}

        #endregion
    }
}